package com.yellow.car.task;

import com.yellow.car.api.DispatchApi;
import com.yellow.car.api.StationApi;
import com.yellow.car.api.YellowCarApi;
import com.yellow.car.entity.CyclingTruckCar;
import com.yellow.car.entity.CyclingYellowCar;
import com.yellow.car.entity.Station;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;

@Configuration
@EnableScheduling
@Slf4j
public class LogTask {
    @Resource
    StationApi stationApi;
    @Resource
    YellowCarApi yellowCarApi;
    @Resource
    DispatchApi dispatchApi;

    @Scheduled(cron = "0/1 * * * * ?")
    private void log() {
        List<Station> stationList = stationApi.getStationInfo();
        StringBuilder sb = new StringBuilder();
        if (!CollectionUtils.isEmpty(stationList)) {
            stationList.stream().forEach(item -> {
                sb.append("站点名称").append(item.getStationName())
                        .append("站点车数量").append(item.getCarNum())
                        .append("站点货车数量").append(Optional.ofNullable(item.getTruckList()).map(List::size).orElse(0));
            });
        }
        List<CyclingYellowCar> carList = yellowCarApi.getCyclingCar();
        sb.append("路上车数量").append(carList.size());
        List<CyclingTruckCar> cyclingTruckCarList = dispatchApi.getCyclingTruckList();
        AtomicInteger truckCarNum = new AtomicInteger();
        cyclingTruckCarList.stream().forEach(item -> {
            truckCarNum.set(item.getYellowCarNum() + truckCarNum.get());
        });
        sb.append("货车运输数量").append(truckCarNum.get());
        log.info("实时日志:{}", sb);
    }
}
